package sktit.practice.jpaint;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import sktit.practice.jpaint.canvas.Canvas;
import sktit.practice.jpaint.canvas.RectangleCanvas;
import sktit.practice.jpaint.shape.Rectangle;
import sktit.practice.jpaint.shape.Shape;
import sktit.practice.jpaint.util.ShapeUtils;

public class DrawExample {

	private static Point firstPos;
	private static Point secondPos;
	private static Point tmpPos;
	
	private static Shape shape;
	private static ArrayList<Canvas> canvasManager = new ArrayList<Canvas>();
	
	private static boolean isAllow = false;

	private static boolean isRectangle = false;
	private static boolean isCircle = false;
	private static boolean isEclipse = false;

	public static void main(String[] args) {
		final Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setText("Draw Example");
		shell.setBackground(display.getSystemColor(SWT.COLOR_WHITE));

		firstPos = new Point(0, 0);
		secondPos = new Point(0, 0);
		tmpPos = new Point(0, 0);
		isAllow = false;

		final Button recBtn = new Button(shell, SWT.PUSH);
		recBtn.setText("Rectangle");
		recBtn.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				isRectangle = true;
				isEclipse = false;
				isCircle = false;
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				isRectangle = true;
				isEclipse = false;
				isCircle = false;

			}
		});
		recBtn.pack();

		final Button eclipseBtn = new Button(shell, SWT.PUSH);
		eclipseBtn.setText("Eclipse");
		eclipseBtn.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				isRectangle = false;
				isEclipse = true;
				isCircle = false;
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				isRectangle = false;
				isEclipse = true;
				isCircle = false;

			}
		});
		eclipseBtn.pack();

		
		
		final Button circleBtn = new Button(shell, SWT.PUSH);
		circleBtn.setText("Circle");
		circleBtn.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				isRectangle = false;
				isEclipse = false;
				isCircle = true;

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				isRectangle = false;
				isEclipse = false;
				isCircle = true;

			}
		});
		circleBtn.pack();

		shell.addPaintListener(new PaintListener() {

			@Override
			public void paintControl(PaintEvent arg0) {
				if (isAllow) {
					GC gc = arg0.gc;
					gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
					
					if (isRectangle)
					{
						gc.drawRectangle(firstPos.x, firstPos.y, secondPos.x
								- firstPos.x, secondPos.y - firstPos.y);
						
					}
					else if (isEclipse)
						gc.drawOval(firstPos.x, firstPos.y, secondPos.x
								- firstPos.x, secondPos.y - firstPos.y);
					else if (isCircle)
						gc.drawOval(firstPos.x, firstPos.y, secondPos.x
								- firstPos.x, secondPos.x - firstPos.x);
					for(int i = 0; i < canvasManager.size(); i++)
						canvasManager.get(i).draw();
				}

			}
		});

		shell.addMouseListener(new MouseListener() {

			@Override
			public void mouseUp(MouseEvent arg0) {
				secondPos.x = arg0.x;
				secondPos.y = arg0.y;
				
				isAllow = false;
				ArrayList<Point> vertex = new ArrayList<Point>();
				
				vertex.add(secondPos);
				vertex.add(new Point(firstPos.x, secondPos.y));
				vertex.add(firstPos);
				vertex.add(new Point(secondPos.x, firstPos.y));
				
				
				shape = new Rectangle(vertex);
				
			}

			@Override
			public void mouseDown(MouseEvent arg0) {
				firstPos.x = arg0.x;
				firstPos.y = arg0.y;
				
				secondPos.x = arg0.x;
				secondPos.y = arg0.y;
				
				tmpPos.x = arg0.x;
				tmpPos.y = arg0.y;
				
				isAllow = isRectangle || isCircle || isEclipse;
			}

			@Override
			public void mouseDoubleClick(MouseEvent arg0) {

			}
		});

		shell.addMouseMoveListener(new MouseMoveListener() {

			@Override
			public void mouseMove(MouseEvent arg0) {

				if (isAllow) {
					GC gc = new GC(shell);
					
					gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
					if (isRectangle) {
						
						gc.drawRectangle(ShapeUtils.generate(firstPos, tmpPos));
						gc.setForeground(display
								.getSystemColor(SWT.COLOR_BLACK));
						gc.drawRectangle(ShapeUtils.generate(new Point(arg0.x, arg0.y), firstPos));
						gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
						for(int i = 0; i < canvasManager.size(); i++)
							canvasManager.get(i).draw();
						
						
					}
/*					else if (isEclipse) {
						gc.drawOval(firstPos.x, firstPos.y, tmpPos.x
								- firstPos.x, tmpPos.y - firstPos.y);
						gc.setForeground(display
								.getSystemColor(SWT.COLOR_BLACK));
						gc.drawOval(firstPos.x, firstPos.y,
								arg0.x - firstPos.x, arg0.y - firstPos.y);
					} else if (isCircle) {
						gc.drawOval(firstPos.x, firstPos.y, tmpPos.x
								- firstPos.x, tmpPos.x - firstPos.x);
						gc.setForeground(display
								.getSystemColor(SWT.COLOR_BLACK));
						gc.drawOval(firstPos.x, firstPos.y,
								arg0.x - firstPos.x, arg0.x - firstPos.x);
					}
*/
					tmpPos.x = arg0.x;
					tmpPos.y = arg0.y;
					
				}

			}
		});

		shell.setLayout(new RowLayout());
		shell.open();
		shell.setSize(400, 400);

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
